package com.xx.ems.util.impl;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.util.StringUtils;

import com.xx.ems.util.ObjectTransform;

public class ObjectTransformImpl implements ObjectTransform {
	/* (non-Javadoc)
	 * @see org.ems.xx.util.ObjectTransform#mapToJavaBean(java.lang.Class, java.util.Map)
	 */
	@Override
	public Object mapToJavaBean(Class<?> target, Map<String, Object> map) {
		try{
			//创建目标类的实例
			Object obj = target.newInstance();
			Set<String> key = map.keySet();
			for(String field : key){
				//首字母大写
				String objectfield = StringUtils.capitalize(field);
				//获取get方法
				Method getmethod = target.getDeclaredMethod("get"+objectfield);
				//得到返回类型(即相应成员变量的数据类型)
				Class<?> type = getmethod.getReturnType();
				//获取set方法
				Method setmethod = target.getDeclaredMethod("set"+objectfield,type);
				//根据相应的数据类型对数据进行类型转换后调用类的set方法填充数据
				if(type.equals(String.class)) setmethod.invoke(obj, String.valueOf(map.get(field))); 
				else if(type.equals(Integer.class)) setmethod.invoke(obj, Integer.valueOf((String) map.get(field))); 
				else if(type.equals(Double.class)) setmethod.invoke(obj, Double.valueOf((String) map.get(field))); 
				else if(type.equals(Date.class)) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					setmethod.invoke(obj, sdf.parse((String) map.get(field)));
				}
			}
			return obj;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.ems.xx.util.ObjectTransform#javabeanToMap(java.lang.Object)
	 */
	@Override
	public Map<String, Object> javabeanToMap(Object object) {
		try{
			Map<String,Object> map = new HashMap<String,Object>();
			Class<?> target = object.getClass();
			Field[] fieldarray = target.getDeclaredFields();
			for(Field field : fieldarray){
				//首字母大写
				String objectfield = StringUtils.capitalize(field.getName());
				//获取get方法
				Method getmethod = target.getDeclaredMethod("get"+objectfield);
				//得到相应成员变量的值(没有值为null)
				Object value = getmethod.invoke(object);
				map.put(field.getName(), value);
			}
			return map;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/** 
     * 将一个 JavaBean 对象转化为一个  Map 
     * @param bean 要转化的JavaBean 对象 
     * @return 转化出来的  Map 对象 
     * @throws IntrospectionException 如果分析类属性失败 
     * @throws IllegalAccessException 如果实例化 JavaBean 失败 
     * @throws InvocationTargetException 如果调用属性的 setter 方法失败 
     */  
    @SuppressWarnings({ "rawtypes", "unchecked" })  
    public Map convertBean(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException {  
        Class type = bean.getClass();  
        Map returnMap = new HashMap();  
        BeanInfo beanInfo = Introspector.getBeanInfo(type);  
  
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();  
        for (int i = 0; i< propertyDescriptors.length; i++) {  
            PropertyDescriptor descriptor = propertyDescriptors[i];  
            String propertyName = descriptor.getName();  
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();  
                Object result = readMethod.invoke(bean, new Object[0]);  
                if (result != null) {  
                    returnMap.put(propertyName, result);  
                } else {  
                    returnMap.put(propertyName, "");  
                }  
            }  
        }  
        return returnMap;  
    }
}